package com.quanyan.place.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.place.entity.apireq.ReqPlaceBundle;
import com.quanyan.place.service.PlaceBundleSaleService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * Created by youxiaojia on 2016/10/12.
 */
@RestController
@RequestMapping("/admin/api/place/bundleSale")
public class PlaceBundleSaleController {
    @Autowired
    private PlaceBundleSaleService placeBundleSaleService;

    /**
     * 获取场馆销售时段
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPlaceSaleTimes", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getPlaceSaleTimes(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId =  MapUtils.getInteger(map, "placeId");
        Integer categoryId =  MapUtils.getInteger(map, "categoryId");
        APIResponse apiResponse = placeBundleSaleService.getPlaceSaleTimes(placeId, categoryId);
        return apiResponse;
    }

    /**
     * 设置捆绑销售
     * @param reqPlaceBundle
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/settingBundleSale", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> settingBundleSale(@RequestBody @Valid ReqPlaceBundle reqPlaceBundle, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        APIResponse apiResponse = null;
        try {
            apiResponse = placeBundleSaleService.settingBundleSale(reqPlaceBundle);
        } catch (Exception e) {
            return APIResponse.returnFail(e.getMessage());
        }
        return apiResponse;
    }

    /**
     * 获取捆绑数据
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getBundleSale", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getBundleSale(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId =  MapUtils.getInteger(map, "placeId");
        Integer categoryId = MapUtils.getInteger(map,"categoryId");
        APIResponse apiResponse = placeBundleSaleService.getBundleSale(placeId,categoryId);
        return apiResponse;
    }

    /**
     * 取消捆绑销售
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/cancelBundleSale", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> cancelBundleSale(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer bundleNo =  MapUtils.getInteger(map, "bundleNo");
        APIResponse apiResponse = null;
        try {
            apiResponse = placeBundleSaleService.cancelBundleSale(bundleNo);
        } catch (Exception e) {
            return APIResponse.returnFail(e.getMessage());
        }
        return apiResponse;
    }
}
